package com.sunshine.lnuplus.view

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatDelegate
import androidx.preference.CheckBoxPreference
import androidx.preference.ListPreference
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import com.sunshine.lnuplus.R
import com.sunshine.lnuplus.callback.TimetableSettingCallBack
import com.sunshine.lnuplus.model.InitData
import com.sunshine.lnuplus.ui.activity.ShortCutActivity
import com.sunshine.lnuplus.ui.activity.SplashActivity
import com.sunshine.lnuplus.ui.activity.TimetableThemeActivity
import com.sunshine.lnuplus.utils.TagHelper
import es.dmoral.toasty.Toasty

class AppSettingView() : PreferenceFragmentCompat(), Preference.OnPreferenceClickListener, Preference.OnPreferenceChangeListener{
    private var callBack: TimetableSettingCallBack? = null
    override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
        setPreferencesFromResource(R.xml.setting_app, rootKey)

        preferenceManager.findPreference<Preference>("timetable_theme")?.apply {
            onPreferenceClickListener = this@AppSettingView
        }
        preferenceManager.findPreference<ListPreference>("theme")?.apply {
            onPreferenceChangeListener = this@AppSettingView
        }
        preferenceManager.findPreference<Preference>("shortcut")?.apply {
            onPreferenceClickListener = this@AppSettingView
        }
//        preferenceManager.findPreference<CheckBoxPreference>("night_auto")?.apply {
//            if(this.isChecked) {
//                val night = preferenceManager.findPreference<CheckBoxPreference>("night")
//                night?.apply {
//                    isEnabled = false
//                    summary = "跟随系统模式下不可设置"
//                }
//            }
//            onPreferenceChangeListener = this@AppSettingView
//        }
        preferenceManager.findPreference<CheckBoxPreference>("night")?.apply {
            onPreferenceChangeListener = this@AppSettingView
        }
        preferenceManager.findPreference<CheckBoxPreference>("navigation")?.apply {
            onPreferenceChangeListener = this@AppSettingView
        }
    }

    fun setCallBack(callBack: TimetableSettingCallBack){
        this.callBack = callBack
    }

    override fun onPreferenceClick(preference: Preference?): Boolean {
        when(preference?.key){
            "timetable_theme" -> {
                activity?.startActivityForResult(Intent(activity, TimetableThemeActivity::class.java), TagHelper.APPSETTING)
            }
            "shortcut" -> {
                startActivity(Intent(activity, ShortCutActivity::class.java))
            }
        }
        return true
    }

    override fun onPreferenceChange(preference: Preference?, newValue: Any?): Boolean {
        when(preference?.key){
//            "night_auto" -> {
//                val night = preferenceManager.findPreference<CheckBoxPreference>("night")
//                val auto = newValue as Boolean
//                if(auto){
//                    night?.apply {
//                        isEnabled = false
//                        summary = "跟随系统模式下不可设置"
//                    }
//                }else{
//                    night?.apply {
//                        isEnabled = true
//                        summary = "部分界面重启后生效"
//                    }
//                }
//
//            }
            "night" -> {
                if(newValue as Boolean){
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
                }else{
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
                    InitData.lessonStyle.alpha = context!!.getSharedPreferences("com.sunshine.lnuplus_preferences",
                        Context.MODE_PRIVATE).getInt("alpha", 200)
                }
                callBack?.onChanged()
            }
            "theme", "navigation" -> {
                callBack?.onChanged()
            }
        }
        return true
    }
}